import webbrowser
import requests
import tkinter as tk
from tkinter import ttk

class Template(object):

    def getConfig(self, data):
        # 创建主窗口
        root = tk.Tk()
        root.title("欢迎使用抢票系统！")
        root.geometry("400x300")

        # 创建样式
        style = ttk.Style()
        style.configure('TLabel', font=('Arial', 12))
        style.configure('TEntry', background='lightgray')
        style.configure('TButton', foreground='white', background='blue')

        number = 1
        entryArray = []
        for row in data:
            # 创建标签和输入框
            label = "label" + str(number)
            entry = "entry" + str(number)

            if row['text'] != 'null':
                text = "请输入：" + row['label'] + "(" + row['text'] + ")"
            else:
                text = "请输入：" + row['label']
            label = ttk.Label(root, text = text)
            label.pack()

            entry = ttk.Entry(root)
            entry.pack()
            entryArray.append({"index":row['index'] , "entry":entry})
            number = number + 1
        
        # 创建按钮
        configData = []
        def submit():
            for row in entryArray:
                configData.append({"index":row['index'] , "value":row['entry'].get()})
            root.destroy()
            return configData
        button = ttk.Button(root, text="提交", command=submit, style='TButton')
        button.pack()

        # 运行主循环
        root.mainloop()


    def password_get(self, data):
        top = tk.Toplevel()
        top.title("输入框")
        # 窗口大小及生成位置偏移
        screenwidth = top.winfo_screenwidth()
        screenheight = top.winfo_screenheight()
        size_geo = '%dx%d+%d+%d' % (600, 500, (screenwidth-300)/2, (screenheight-300)/2) 
        top.geometry(size_geo) 
        
        top.resizable(0,0)
        top.attributes('-topmost',True) # 设置窗口处于顶层

        number = 1
        entryArray = []
        for row in data:
            # 创建标签和输入框
            label = "label" + str(number)
            entry = "entry" + str(number)

            if row['text'] != 'null':
                text = "请输入：" + row['label'] + "(" + row['text'] + ")"
            else:
                text = "请输入：" + row['label']
            label = ttk.Label(top, text = text)
            label.pack()

            entry = ttk.Entry(top)
            entry.pack()
            entryArray.append({"index":row['index'] , "entry":entry})
            number = number + 1

        # label = tk.Label(top, text="请输入密码:",font=(10))
        # label.pack(pady=20)
        
        # input_var = tk.StringVar()
        # entry = tk.Entry(top, show="*", textvariable=input_var)
        # entry.pack()
        
        configData = []
        def confirm():
            for row in entryArray:
                configData.append({"index":row['index'] , "value":row['entry'].get()})
            top.destroy()
            return configData
            # value = input_var.get()
            # top.destroy()
            # return value
        
        button_frame = tk.Frame(top)
        button_frame.pack(pady=10)
        
        confirm_button = tk.Button(button_frame, text="确认", command=confirm,width=10,height=1,activeforeground="#FF0000")
        confirm_button.pack(side=tk.LEFT, padx=5)
        
        cancel_button = tk.Button(button_frame, text="取消", command=top.destroy,width=10,height=1,activeforeground="#FF0000")
        cancel_button.pack(side=tk.LEFT, padx=5)
        
        top.bind("<Return>", lambda event: confirm())
        top.bind("<Escape>", lambda event: top.destroy())
        
        top.focus_set()
        top.grab_set()
        top.wait_window()
        
        return configData